package com.example.lyl.yuejian.view.club;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.ClubDetialBean;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ClubDetialActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.club_iv)
    ImageView clubIv;
    @BindView(R.id.club_name_tv)
    TextView clubNameTv;
    @BindView(R.id.club_des_tv)
    TextView clubDesTv;
    @BindView(R.id.club_address_tv)
    TextView clubAddressTv;
    @BindView(R.id.activity_tv)
    TextView activityTv;
    @BindView(R.id.ygz_tv)
    TextView ygzTv;
    @BindView(R.id.djgz_btn)
    Button djgzBtn;

    private int cid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detial);
        ButterKnife.bind(this);

        toolbar.setTitle("俱乐部详情");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        cid = getIntent().getIntExtra("cid",0);

        NetTool.getInstance().getApi()
                .clubDetialRequest(cid, SharePreferenceUtils.getInt(this,"uid",0))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ClubDetialBean>() {
                    @Override
                    public void accept(ClubDetialBean clubDetialBean) throws Exception {
                        Glide.with(ClubDetialActivity.this).load("http://" + clubDetialBean.getData().getCpic()).placeholder(R.mipmap.lunbo1).into(clubIv);
                        clubNameTv.setText("俱乐部名称:  " + clubDetialBean.getData().getCname());
                        clubDesTv.setText("俱乐部简介:  " + clubDetialBean.getData().getCintro());
                        clubAddressTv.setText("地址:  " + clubDetialBean.getData().getCplace());
                        activityTv.setText("最近活动:  " + clubDetialBean.getData().getCactivity());
                        if (clubDetialBean.getData().isIsAttention()){
                            djgzBtn.setVisibility(View.GONE);
                            ygzTv.setVisibility(View.VISIBLE);
                        }else {
                            djgzBtn.setVisibility(View.VISIBLE);
                            ygzTv.setVisibility(View.GONE);
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return  true;
    }

    @OnClick(R.id.djgz_btn)
    public void onViewClicked() {
        NetTool.getInstance().getApi()
                .attationClubRequest(cid,SharePreferenceUtils.getInt(this,"uid",0))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResultBean>() {
                    @Override
                    public void accept(ResultBean resultBean) throws Exception {
                        djgzBtn.setVisibility(View.GONE);
                        ygzTv.setVisibility(View.VISIBLE);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }
}
